#ifndef SERIALPORT_H
#define SERIALPORT_H

#include <QObject>
#include <QSerialPort>
#include <QSerialPortInfo>
#include <QByteArray>

class SerialPort : public QObject
{
    Q_OBJECT
    Q_PROPERTY(bool isConnected READ isConnected NOTIFY isConnectedChanged)
    Q_PROPERTY(QStringList availablePorts READ availablePorts NOTIFY availablePortsChanged)

public:
    explicit SerialPort(QObject *parent = nullptr);
    ~SerialPort();

    bool isConnected() const;
    QStringList availablePorts();  // 删除 const 限定符

public slots:
    bool connectToPort(const QString &portName, int baudRate, int dataBits, int parity, int stopBits);
    void disconnectPort();
    void sendData(const QByteArray &data);

signals:
    void isConnectedChanged();
    void availablePortsChanged();
    void dataReceived(const QByteArray &data);
    void errorOccurred(const QString &error);

private:
    QSerialPort *m_serialPort;
    mutable QStringList m_availablePorts;  // 使用 mutable 允许在 const 方法中修改
};

#endif // SERIALPORT_H
